import Vue from 'vue'
import VueRouter from 'vue-router'
import * as auth from '../utils/auth'
import store from '../store'
import setting from '../setting'
import Login from '../pages/login/index.vue'

Vue.use(VueRouter)

const customRoutes = [
  {
    path: '/',
    redirect: {
      name: setting.app_index_page
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../pages/home/index.vue'),
    meta: {
      title: '首页'
    },
    children: [
      {
        path: '/menu',
        name: 'Menu',
        component: () => import('../pages/menu/index.vue'),
        meta: {
          title: '菜单管理'
        }
      },
      {
        path: '/user',
        name: 'User',
        component: () => import('../pages/user/index.vue'),
        meta: {
          title: '用户列表'
        }
      },
      {
        path: '/role',
        name: 'Role',
        component: () => import('../pages/role/index.vue'),
        meta: {
          title: '角色管理'
        }
      },
      {
        path: '/device',
        name: 'Device',
        component: () => import('../pages/device/index.vue'),
        meta: {
          title: '设备管理'
        }
      },
      {
        path: '/device_type',
        name: 'DeviceType',
        component: () => import('../pages/device_type/index.vue'),
        meta: {
          title: '设备类型'
        }
      },
      {
        path: '/group',
        name: 'Group',
        component: () => import('../pages/group/index.vue'),
        meta: {
          title: '小组管理'
        }
      },
      {
        path: '/addGroup',
        name: 'AddGroup',
        component: () => import('../pages/group/childrenPage/addGroup.vue'),
        meta: {
          title: '添加小组'
        }
      },
      {
        path: '/userinfo',
        name: 'Userinfo',
        component: () => import('../pages/userinfo/index.vue'),
        meta: {
          title: '角色管理'
        }
      },
      {
        path: '/company',
        name: 'Company',
        component: () => import('../pages/company/index.vue'),
        meta: {
          title: '客户公司列表'
        }
      },
      {
        path: '/project',
        name: 'Project',
        component: () => import('../pages/project/index.vue'),
        meta: {
          title: '工程管理'
        }
      },
      {
        path: '/Position',
        name: 'Position',
        component: () => import('../pages/position/index.vue'),
        meta: {
          title: '工程详情'
        }
      },
      {
        path: '/datumPoint',
        name: 'DatumPoint',
        component: () => import('../pages//datumPoint/index.vue'),
        meta: {
          title: '基准点'
        }
      },
      {
        path: '/Unit',
        name: 'Unit',
        component: () => import('../pages/unit/index.vue'),
        meta: {
          title: '监测单元'
        }
      },
      {
        path: '/project_data',
        name: 'ProjectData',
        component: () => import('../pages/project_data'),
        meta: {
          title: '工程实时数据'
        }
      },
      {
        path: '/project_data_chart',
        name: 'ProjectDataChart',
        component: () => import('../pages/project_data_chart'),
        meta: {
          title: '工程实时数据'
        }
      },
      {
        path: '/report',
        name: 'Report',
        component: () => import('../pages/report'),
        meta: {
          title: '预警信息'
        }
      },
      {
        path: '/dictionaries',
        name: 'Dictionaries',
        component: () => import('../pages/dictionaries'),
        meta: {
          title: '字典管理'
        }
      }
    ]
  }
]

const defaultRoutes = [
  {
    path: '/403',
    name: '403',
    component: () => import('../pages/error/403.vue'),
    meta: {
      title: '403'
    }
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../pages/error/404.vue'),
    meta: {
      title: '404'
    }
  },
  {
    path: '/500',
    name: '500',
    component: () => import('../pages/error/500.vue'),
    meta: {
      title: '500'
    }
  }
]

const routes = [...customRoutes, ...defaultRoutes]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name === setting.app_index_page) next()
  if (auth.getToken() === null) {
    next({
      name: setting.app_index_page
    })
  } else {
    next()
  }
  if (to.meta.title) {
    document.title = `${to.meta.title} - ${setting.app_name}`
  } else {
    document.title = `${setting.app_name}`
  }
})

export default router
